from flask import Flask, render_template
from conredis import Redis
from flask import jsonify
app = Flask(__name__)
app.config['REDIS_HOST'] = "127.0.0.1" # redis数据库地址
app.config['REDIS_PORT'] = 6379 # redis 端口号
app.config['REDIS_DB'] = 0 # 数据库名
a=0
# 注册接口
app.config['JSON_AS_ASCII'] = False
app.config['DEBUG'] = True
@app.route('/index')
def indexs():
    return render_template("main.html")

@app.route('/count')
def count1():
    count = Redis.count("rep")
    return count

@app.route('/l1')
def index2():
    count = Redis.rank("book_rank")
    re = []
    re1 = []
    for i in count:
        re.append(str(i[0], "utf-8")[0:4])
        re1.append(int(i[1]))
    return jsonify({"data": re, "value": re1})

@app.route('/r1')
def index3():
    count = Redis.rank("press_rank")
    re = []
    re1 = []
    for i in count:
        re.append(str(i[0], "utf-8")[0:4])
        re1.append(int(i[1]))
    return jsonify({"data": re,"value":re1})


@app.route('/c2')
def index4():
    count = Redis.all("place_rank")
    re = []
    for i in count:
        if(str(i[0],"utf-8")=="黑龙"):
            re.append({"name": "黑龙江", "value": int(i[1])})
        elif(str(i[0],"utf-8")=="内蒙"):
            re.append({"name": "内蒙古", "value": int(i[1])})
        else:
            re.append({"name": str(i[0], "utf-8"), "value": int(i[1])})

    return jsonify({"data": re})


@app.route('/r2')
def indexasdw():
    count = Redis.city_rank("zf_rank")
    re = []
    for i in count:
            re.append({"name": str(i[0], "utf-8"), "value": int(i[1])})

    return jsonify({"data": re})


@app.route('/c1')
def index():
    total = Redis.read("total")
    sum = Redis.read("order_sum")
    count = Redis.count("rep")
    return jsonify({"data": round(float(total)),"data1":int(sum),"data2":int(count)})


@app.route('/l2')
def indexsp(a=a):
    speed = Redis.read("speed")
    return jsonify({"value": int(speed)})


if __name__=="__main__":
	app.run()